package com.atguigu.gmall.list.listerner;

import com.atguigu.gmall.list.service.ListService;
import com.atguigu.gmall.rebbit.constant.MqConst;
import com.rabbitmq.client.Channel;

import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.IOException;

@Component
public class ConsumerUpdataEs {

    @Autowired
    private ListService listService;
    //上架 添加es

    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = MqConst.QUEUE_GOODS_UPPER,durable ="true",autoDelete = "false"),
            exchange = @Exchange(value = MqConst.EXCHANGE_DIRECT_GOODS),
            key = {MqConst.ROUTING_GOODS_UPPER}
    ))
    //参数1：消息信息  参数2：请求集合  参数3：响应
    public void upperGoods(Long skuId, Message message, Channel channel){

        //应答

        try {
            listService.upperGoods(skuId);
            //正常响应  参数1：响应头标签 参数2：是否进行批处理
            channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);
        } catch (Exception e) {
            //e.printStackTrace();  是否是第一次
            if(!message.getMessageProperties().isRedelivered()){

                try {
                    //不应答  参数1：标签参数  参数2：是否批处理  参数3：是否放回队列
                    channel.basicNack(message.getMessageProperties().getDeliveryTag(),false,true);
                } catch (IOException ex) {
                    ex.printStackTrace();
                }
            }else {
                try {
                    //拒绝  参数1：标签参数  参数2：是否放回队列
                    channel.basicReject(message.getMessageProperties().getDeliveryTag(),false);
                } catch (IOException ex) {
                    ex.printStackTrace();
                }
            }

        }
    }
    //下架 删除es
    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = MqConst.QUEUE_GOODS_LOWER,durable ="true",autoDelete = "false"),
            exchange = @Exchange(value = MqConst.EXCHANGE_DIRECT_GOODS),
            key = {MqConst.QUEUE_GOODS_LOWER}
    ))
    public void lowerGoods(Long skuId, Message message, Channel channel){

        try {
            listService.lowerGoods(skuId);
            //正常响应  参数1：响应头标签 参数2：是否进行批处理
            channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);
        } catch (Exception e) {
            //e.printStackTrace();  是否是第一次
            if(!message.getMessageProperties().isRedelivered()){

                try {
                    //不应答  参数1：标签参数  参数2：是否批处理  参数3：是否放回队列
                    channel.basicNack(message.getMessageProperties().getDeliveryTag(),false,true);
                } catch (IOException ex) {
                    ex.printStackTrace();
                }
            }else {
                try {
                    //拒绝  参数1：标签参数  参数2：是否放回队列
                    channel.basicReject(message.getMessageProperties().getDeliveryTag(),false);
                } catch (IOException ex) {
                    ex.printStackTrace();
                }
            }

        }
    }
}
